package com.pxxy.app_main;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.pxxy.lib_common.base.BaseActivity;
import com.pxxy.lib_common.base.BaseFragment;
import com.pxxy.lib_common.constant.ArouterURLS;
import com.pxxy.security512.R;


public class HomeActivity extends BaseActivity {
    private boolean isExit = false;
    private BaseFragment fragmentHome,fragmentKnowledge,fragmentMine;
    private RadioGroup homeRadioGroup;
    private FragmentManager fragmentManager;
    EditText phone;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    isExit = false;
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {
        fragmentHome = (BaseFragment) ARouter.getInstance().build("/home/fragmentHome").navigation();
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.home_top_framelayout, fragmentHome);
        transaction.commit();
        homeRadioGroup = findViewById(R.id.home_rg_radiogroup);
    }

    @Override
    public void initEvent() {
        homeRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                //隐藏所有的fragment
                hideAllFragment(transaction);
                if (checkedId == R.id.home_rb_home){
                    if (fragmentHome == null) {
                        fragmentHome = (BaseFragment) ARouter.getInstance().build(ArouterURLS.FRAGMENT_HOME_AROUTER_URL).navigation();
                        transaction.add(R.id.home_top_framelayout, fragmentHome);
                    } else {
                        transaction.show(fragmentHome);
                    }
                } else if (checkedId == R.id.home_rb_knowledge) {
                    if (fragmentKnowledge == null) {
                        fragmentKnowledge = (BaseFragment) ARouter.getInstance().build(ArouterURLS.FRAGMENT_KNOWLEDGE_AROUTER_URL).navigation();
                        transaction.add(R.id.home_top_framelayout, fragmentKnowledge);
                    } else {
                        transaction.show(fragmentKnowledge);
                    }
                }else if (checkedId == R.id.home_rb_mine){
                    if (fragmentMine == null){
                        fragmentMine = (BaseFragment) ARouter.getInstance().build(ArouterURLS.FRAGMENT_MINE_AROUTER_URL).navigation();
                        transaction.add(R.id.home_top_framelayout,fragmentMine);
                    }else{
                        transaction.show(fragmentMine);
                    }
                }
                transaction.commit();
            }
        });
    }

    /**
     * 隐藏所有的fragment
     *
     * @param fragmentTransaction
     */
    public void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (fragmentHome != null) {
            fragmentTransaction.hide(fragmentHome);
        }

        if (fragmentKnowledge != null) {
            fragmentTransaction.hide(fragmentKnowledge);
        }

        if (fragmentMine != null) {
            fragmentTransaction.hide(fragmentMine);
        }
    }


    @Override
    public int initLayout() {
        return R.layout.activity_home;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //按返回键不销毁activity
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isExit) {
                isExit = true;
                toastShort("再按一次退出程序");
                handler.sendEmptyMessageDelayed(1, 2000);
            } else {
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
